<template>
    <div class="login">
        <div class="login-box">
            <div class="logo">
                <img src="../assets/img/un.jpg" alt="">
            </div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="login-form">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" autocomplete="off"
                    type="text" prefix-icon="el-icon-user"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="ruleForm.password" autocomplete="off"
                    type="password" prefix-icon="el-icon-unlock"></el-input>
                </el-form-item>
                <el-form-item class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button type="info" @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { LoginPost } from "../request/api";
export default {
    name: "login",
    data(){
        return{
            ruleForm: {
                username: '',
                password: '',
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur' }
                ],
            }
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    console.log(this.ruleForm)
                    LoginPost (this.ruleForm).then(res => {
                        // 成功之后的操作
                        if(res.meta.status != 200){//登陆失败返回
                            this.$message.error(res.meta.msg);
                        }else{
                            this.$message.success(res.meta.msg);
                            localStorage.setItem('token',res.data.token);
                            this.$router.push('./home')
                        }
                    })
                } else {
                    this.$message.error('输入错误！');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }

}
</script>

<style scoped lang="less">
.login{
    position: relative;
    height: 100%;
    background: rgb(0, 65, 126);
    .login-box{
        display: flex;
        align-items: center;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -150px;
        margin-left: -235px;
        padding: 0 20px;
        width: 450px;
        height: 300px;
        background: #fff;
        border-radius: 3px;
        .logo{
            z-index: 1;
            position: absolute;
            top: -40px;
            left: 50%;
            margin-left: -40px;
            width: 80px;
            height: 80px;
            border-radius: 50%;
            border: 4px solid #fff;
            overflow: hidden;
            img{
                width: 100%;
            }
        }
        .login-form{
            width: 100%;
            .login-btn{
                display: flex;
                justify-content: center;
                margin-bottom: 0;
            }
        }
    }
}
</style>